package com.ftqh.tutorial.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * Created by Administrator on 2017/4/27.
 */
@RestController
public class ConsumerController {
    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/add")
    public String add() {
        return restTemplate.getForEntity("http://COMPUTE-SERVICE/add?a=10&b=20", String.class).getBody();
    }

    @GetMapping("/user")
    public String getUser() {
        ResponseEntity<JSONObject> jsonObject = restTemplate.getForEntity("http://COMPUTE-SERVICE/users/1", JSONObject.class);
        return jsonObject.getBody().toJSONString();
    }
    @GetMapping("/users1")
    public JSONArray getUsers1(){
        Integer[] ids = {1,2};
        JSONObject idsJson = new JSONObject();
        idsJson.put("ids",ids);
        ResponseEntity<JSONArray> jsonObject = restTemplate.postForEntity("http://COMPUTE-SERVICE/users1",idsJson, JSONArray.class);
        return jsonObject.getBody();
    }
    @GetMapping("/users2")
    public JSONArray getUsers2(){
        Integer[] ids = {1,2};
        HttpEntity<Integer[]> requestEntity = new HttpEntity<>(ids);
        JSONArray jsonObject = restTemplate.postForObject("http://COMPUTE-SERVICE/users2", requestEntity, JSONArray.class);
        return jsonObject;
    }
}
